// pages/professInfos/professInfos.js
var app = getApp();
const isTel = (value) => !/^1[34578]\d{9}$/.test(value)
Page({
  onChange(e) {
      // console.log('onChange', e)
      this.setData({
          error: isTel(e.detail.value),
          value: e.detail.value,

      })
  },
  nextPage() {
    swan.navigateTo({
        url: '/packageA/pages/success/success'
    });
  },
  onFocus(e) {
      this.setData({
          error: isTel(e.detail.value),
      })
      // console.log('onFocus', e)
  },
  onBlur(e) {
      this.setData({
          error: isTel(e.detail.value),
      })
      // console.log('onBlur', e)
  },
  onConfirm(e) {
      console.log('onConfirm', e)
  },
  onClear(e) {
      // console.log('onClear', e)
      this.setData({
          error: true,
          value: '',
      })
  },
  onError() {
      wx.showModal({
          title: 'Please enter 11 digits',
          showCancel: !1,
      })
  },
  /**
   * 页面的初始数据
   */
  /**
    * 页面的初始数据
    */
  data: {
    items:[
      { name: 'girl', value: '女',checked: 'true' },
      { name: 'boy', value: '男',  },
    ],
    professRecordVo:{
        toName: '',
        toPhone: '',
        toEmail: ''
    },
    professRecordEntity:'',
    profess:''
  },

  getToName(e){
    this.setData({
        'professRecordVo.toName':e.detail.value
    })
  },

  getToEmail(e){
    this.setData({
        'professRecordVo.toEmail':e.detail.value
    })
  },
  getToPhone(e){
    this.setData({
        'professRecordVo.toPhone':e.detail.value
    })
  },
  getToVxQq(e){
    this.setData({
        'professRecordVo.toVxQq':e.detail.value
    })
  },

  getProfess(e){
    this.setData({
        'profess':e.detail.value
    })
  },

  addProfessRecordVo(){

        // console.log('sendInfo',this.data.professRecordVo)
        console.log(app.cache.get('loginUser'));
        if(this.data.professRecordVo.toName == '' || this.data.professRecordVo.toPhone == '' ||
        this.data.professRecordVo.toEmail == ''){
            app.showToastFail('请填写完整！')
            return;
        }
        

        this.setData({
            'professRecordVo.fromId':app.cache.get('loginUser').id
        })

        swan.request({
            url: app.globalData.URL + "/8100/profess-record/add",
            method:'post',
            data:this.data.professRecordVo,
            success:res=>{

                console.log("发送成功:",res)

                if(res.data.code != 200){
                    app.showToastFail(res.data.msg)
                    return;
                }

                app.showToastSuccess("发送成功！")

                // 保存profess
                this.setData({
                    professRecordEntity:res.data.data
                })

                this.updateProfess()

                swan.navigateTo({
                    url: '../success/success'
                });
            },
            fail:err=>{
                console.log(err)
                //app.showToastFail('失败...')
            }
        })
  },

  updateProfess(){

        if(this.data.profess == '' || this.data.profess == undefined ){
            return;
        }

        this.setData({
            'professRecordEntity.profess.context': this.data.profess
        })

        swan.request({
            url: app.globalData.URL + "/8100/profess/update",
            method:'put',
            data:this.data.professRecordEntity.profess,
            success:res=>{

                if(res.data.code != 200){
                    app.showToastFail(res.data.msg)
                    return;
                }
                console.log("profess 保存成功：",res)
            },
            fail:err=>{
                console.log(err)
            }
        })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onLoad: function () {



  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})